package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.icd.service.IcdDiseaseGaogeService;

@Controller
public class IcdDiseaseGaogeController {

	@Autowired
	private IcdDiseaseGaogeService icdDiseaseGaogeService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_gaoge_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("modifyFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		Integer start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "codeSearch");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameSearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String modifyFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "modifyFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdDiseaseGaogeService.list(start, codeSearch, nameSearch, exactSearchFlag, modifyFlag,countPerPageStr);
	}
	
	@ResponseBody
	@RequestMapping(value = "/icd/disease_gaoge_import.do")
	public ReturnData gaogeImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return icdDiseaseGaogeService.diseaseImport(file, user);
	}

	
	@ResponseBody
	@RequestMapping(value = "/icd/disease_gaoge_add.do")
	public ReturnData addGaoge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String newGaogeName = req.getParameter("newGaogeName");
		String newDiseaseName = req.getParameter("newDiseaseName");
		return icdDiseaseGaogeService.addGaogeDisease(newGaogeName ,newDiseaseName , user);
	}
	@RequestMapping(value = "/icd/gaoge_export.htm")
	public void codeExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		icdDiseaseGaogeService.codeExport(resp);
	}
	
	@ResponseBody
	@RequestMapping(value = "/icd/disease_gaoge_delete.do")
	public ReturnData deleteGaoge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = Long.parseLong(req.getParameter("id"));
		return icdDiseaseGaogeService.deleteDisease(id);
	}
	
}
